package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.service.SMSService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发送验证码
 */
@RestController
@RequestMapping("sms")
@Slf4j
public class SendCodeController {
    @Autowired
    private SMSService smsService;

    /**
     * 发送登入验证码
     * @param phone
     * @return
     */
    @PostMapping("sendLoginCode")
    public Result sendLoginCode(String phone){
        String validateCode = smsService.sendSms(phone);

        if (null == validateCode) {
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,validateCode);
    }

    /**
     * 发送预约验证码
     * @param phone
     * @return
     */
    @PostMapping("sendCode")
    public Result sendCode(String phone){
        String validateCode = smsService.sendSms(phone);

        if (null == validateCode) {
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,validateCode);
    }
}
